package com.xiaoxi.compositemode.composite;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @Author xiaoxi
 * @ProjectName design_mode
 * @PackageName com.xiaoxi.compositemode.composite
 * @ClassName Directory
 * @Description TODO
 * @Date 2023/3/14 10:04
 * @Version 1.0
 */
public class Directory extends AbstractEntry {

    private String name;
    private List<Entry> directoryList = new ArrayList<>();

    public Directory(String name) {
        this.name = name;
    }

    @Override
    public void add(Entry entry) {
        directoryList.add(entry);
        entry.parent = this;
    }

    @Override
    public void printList(String prefix) {
        System.out.print(prefix + "/" + name);
        System.out.println(" ( " + getSize() + " )");
        Iterator iterator = directoryList.iterator();
        while (iterator.hasNext()) {
            Entry next = (Entry) iterator.next();
            next.printList(prefix + "/" + name);
        }
    }

    @Override
    public String getName() {
        return name;
    }

    @Override
    public int getSize() {
        int size = 0;
        Iterator iterator = directoryList.iterator();
        while (iterator.hasNext()) {
            Entry next = (Entry) iterator.next();
            size += next.getSize();
        }
        return size;
    }


}
